package com.ztom.test.processor

import cn.hutool.core.bean.BeanUtil
import com.ztom.beans.factory.support.processor.BeanPostProcessor
import kotlin.reflect.KClass
import kotlin.reflect.full.isSuperclassOf

/**
 * @author ZhangTao
 */
class CustomBeanPostProcessor(
    var clazz: String? = null,
    var field: String? = null,
    var value: Any? = null
) : BeanPostProcessor {

    override fun postProcessBeforeInitialization(bean: Any, beanName: String): Any? {
        println("invoke CustomBeanPostProcessor postProcessBeforeInitialization...")

        return bean.apply {
            clazz?.let {
                if (Class.forName(it).kotlin.isSuperclassOf(bean::class))
                    BeanUtil.setFieldValue(bean, field, value)
            }
        }
    }

    override fun postProcessAfterInitialization(bean: Any, beanName: String): Any? {
        println("invoke CustomBeanPostProcessor postProcessAfterInitialization...")
        return null
    }
}